@extends("admin.index")

@section("title","产品编辑")

@section("css")
    <link rel="stylesheet" href="/static/layui/css/layui.css">
    {{--<link rel="stylesheet" href="/static/admin/css/bootstrapValidator.min.css">--}}
    <style>
        .pic img{
            width: 100px;
            height: 70px;
            border: 1px solid gainsboro;
            margin-bottom: 10px;
        }
    </style>
@endsection

@section("content")

    <div class="page-inner">
        <div class="page-header">
            <nav class="breadcrumb">
                <div class="left row">
                    <li class="breadcrumb-item"><a href="#">产品管理</a></li>
                    <li class="breadcrumb-item active" aria-current="page">产品编辑</li>
                </div>
                <div class="right row">
                    <li class="breadcrumb-item"><i class="fa fa-reply"></i></li>
                    <li class="breadcrumb-item active" aria-current="page"><i class="fa fa-refresh"></i> </li>
                </div>
            </nav>
        </div>
        <div class="row">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header">
                        <div class="card-title">
                            <a href="{{route("admin.product.index")}}" class="btn btn-primary btn-sm btn-border">产品列表</a>
                        </div>
                    </div>
                    <form action="{{route('admin.product.update',['product'=>$product->id])}}" method="post" enctype="multipart/form-data" id="defaultForm">

                        @include("admin.common.result")
                        @include("admin.common.validate")

                        @csrf
                        @method('PUT')
                        <div class="card-body">
                            <div class="form-group">
                                <label for="pid">所属分类</label>
                                <select name="cid" class="form-control">
                                    @foreach($cates as $item)
                                        <option value="{{$item->id}}" @if($product->cid == $item->id) selected @endif>{{$item->level == 0 ? "" : "|"}}{{str_repeat("----",$item->level)}}{{$item->name}}</option>
                                    @endforeach
                                </select>
                            </div>
                            <div class="form-group">
                                <label for="title">产品名称</label>
                                <input type="text" class="form-control" value="{{$product->title}}" id="title" name="title" placeholder="请输入标题">
                            </div>
                            <div class="form-group">
                                <label for="keyword">关键字</label>
                                <input type="text" class="form-control" value="{{$product->keyword}}" id="keyword" name="keyword" placeholder="请输入关键字">
                            </div>
                            <div class="form-group">
                                <label for="desc">描述</label>
                                <textarea class="form-control" name="desc" id="desc" rows="3">{{$product->desc}}</textarea>
                            </div>
                            <div class="form-group">
                                <label for="remark">摘要</label>
                                <textarea class="form-control" name="remark" id="remark" rows="3">{{$product->remark}}</textarea>
                            </div>
                            <div class="form-group">
                                <label for="views">浏览次数</label>
                                <input type="text" class="form-control" value="{{$product->views}}" id="views" name="views" placeholder="请输入浏览次数">
                            </div>

                            <div class="form-group pic">
                                <label for="file">图片上传</label>
                                <br>
                                @if(isset($product->pic))
                                    <img src="{{$product->pic}}" id="input-pic">
                                @else
                                    <img src="/static/admin/bootstrap/img/face-bg.png" id="input-pic">
                                @endif
                                <input type="file" class="form-control" value="{{$product->pic}}" id="pic" name="pic">
                            </div>

                            {{--                            <div class="form-group">--}}
                            {{--                                <label for="file">图片上传</label>--}}
                            {{--                                <div class="row">--}}
                            {{--                                    <img src="/static/admin/bootstrap/img/face-bg.png" id="input-pic">--}}
                            {{--                                    <input type="hidden" class="form-control" name="pic" id="pic">--}}
                            {{--                                    <div class="btn btn-secondary" id="pic-btn" style="height: 42px;margin-left: 20px;">上传图片</div>--}}
                            {{--                                </div>--}}
                            {{--                            </div>--}}

                            <div class="form-group">
                                <label class="col-form-label">推荐状态</label>
                                <div class="row">
                                    <div class="col-sm-1 col-sm-2 col-sm-4">
                                        <label class="form-checkbox">
                                            <input type="checkbox" name="incom" value="1" @if($product->incom == 1) checked @endif>
                                            <span class="inner"></span>
                                            <span class="label">推荐</span>
                                        </label>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="contents">内容</label>
                                <textarea id="contents" name="contents" class="form-control" style="display: none;">{!! $product->contents !!}</textarea>
                            </div>
                        </div>

                        <div class="card-action">
                            <button class="btn btn-success" type="submit">Submit</button>
                            <button class="btn btn-danger">Cancel</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
@endsection

@section("script")
    <script src="/static/layui/layui.js"></script>
    <script src="/static/admin/bootstrap/js/bootstrapValidator.min.js"></script>
    <script>
        var _token = "{{csrf_token()}}";
        var idea   = "news";//存储图片的目录名称
        layui.use(['layedit','upload'], function(){
            var layedit = layui.layedit;
            var upload  = layui.upload;
            layedit.build('contents',{
                tool: [
                    'strong' //加粗
                    ,'italic' //斜体
                    ,'underline' //下划线
                    ,'del' //删除线

                    ,'|' //分割线

                    ,'left' //左对齐
                    ,'center' //居中对齐
                    ,'right' //右对齐
                    ,'link' //超链接
                    ,'unlink' //清除链接
                    ,'face' //表情
                    // ,'image' //插入图片
                    // ,'help' //帮助
                ],
                height: 180 //设置编辑器高度
            }); //建立编辑器

            {{--upload.render({--}}
            {{--    elem: '#pic-btn' //绑定元素--}}
            {{--    ,url: '{{route("admin.upload.store")}}' //上传接口--}}
            {{--    ,data: {_token,idea}--}}
            {{--    ,done: function(res){--}}
            {{--        if(res.status === 200){--}}
            {{--            $("[name='pic']").val(res.data);--}}
            {{--            $("#input-pic").attr("src",res.data);--}}
            {{--        }--}}
            {{--    }--}}
            {{--    ,error: function(){--}}
            {{--        //请求异常回调--}}
            {{--    }--}}
            {{--});--}}
        });

        //监听图片
        $("#pic").change(function () {
            var imgObj = new FileReader();          //创建图片资源对象
            var file   = this.files[0];             //获取图片信息
            var rep = /jpeg|png|gif|jpg|bmp/ig;
            var gStyle = file.type.split("/")[1];
            if(rep.test(gStyle)){
                imgObj.readAsDataURL(file);        //读取图像文件，实现本地预览
                imgObj.onload = function() {
                    $("#input-pic").attr("src",this.result);
                };
            }else{
                $("#input-pic").attr("src","/static/admin/bootstrap/img/face-bg.png").hide();
            }
        });

        $('#defaultForm').bootstrapValidator({
//        live: 'disabled',
            message: '输入的内容无效',
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                title: {
                    validators: {
                        notEmpty: {
                            message: '产品名称不可为空'
                        },
                        //长度规则
                        stringLength: {
                            min: 2, //最小值
                            max: 16,//最大值
                            message: '产品名称的长度在2~16位'
                        },
                    }
                },
                remark: {
                    validators: {
                        notEmpty: {
                            message: '摘要不可为空'
                        },
                        //长度规则
                        stringLength: {
                            max: 100,//最大值
                            message: '摘要的长度不超过100'
                        },
                    }
                },
                views: {
                    validators: {
                        digits :{
                            message: '浏览次数为数值'
                        }
                    }
                },
                pic: {
                    validators: {
                        notEmpty: {
                            message: '摘要不可为空'
                        },
                        file: {
                            extension: 'png,jpg,jpeg',
                            type: 'image/png,image/jpg,image/jpeg,image/gif',
                            maxSize: 5*1024*1024,
                            message: '请上传图片，且大小不可超过5M'
                        }
                    }
                },
                // contents: {
                //     validators: {
                //         notEmpty: {
                //             message: '内容为必填项'
                //         }
                //     }
                // },
            }
        });
    </script>
@endsection
